package com.oms.web.controller.order;

import com.oms.common.core.domain.AjaxResult;
import com.oms.common.core.domain.Ztree;
import com.oms.system.domain.OmsDeptOrderStatus;
import com.oms.system.domain.SysDictData;
import com.oms.system.domain.SysRole;
import com.oms.system.service.IOrderStatusDeptService;
import com.oms.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/***
 * 为部门分配订单状态查看权限
 * @desc 哪一个部门可以查看什么状态下的订单
 * @author lucas
 * @date 2019-09-08 18:08:05
 */
@Controller
@RequestMapping("/order/orderStatusDeptRelation")
public class OrderStatusDeptRelationController {

    @Autowired
    private ISysDictDataService dictDataService;
    @Resource
    private IOrderStatusDeptService orderStatusDeptService;

    @GetMapping("/{id}")
    public String index(@PathVariable("id") Long id, String deptName, ModelMap mmap){
        mmap.put("deptId", id);
        mmap.put("deptName", deptName);
        return "system/dept/orderStatus";
    }

    /**
     * 加载订单状态列表树
     */
    @GetMapping("/list")
    @ResponseBody
    public List<Ztree> orderStatusTreeData(Long deptId){
        List<Ztree> ztrees = new ArrayList<>();
        SysDictData dictData = new SysDictData();
        dictData.setDictType("business_order_type");
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        Ztree ztree1 = new Ztree();
        ztree1.setId(0L);
        ztree1.setName("订单状态");
        ztree1.setTitle("订单状态");
        ztree1.setOpen(true);
        ztrees.add(ztree1);
        list.stream().forEach(item->{
            Ztree ztree = new Ztree();
            ztree.setId(Long.parseLong(item.getDictValue()));
            ztree.setName(item.getDictLabel());
            ztree.setpId(0L);
            ztree.setTitle(item.getDictLabel());
            //判断是否选中查询关系表
            OmsDeptOrderStatus deptOrder = orderStatusDeptService.getDeptStatusList(deptId, Integer.parseInt(item.getDictValue()));
            if(deptOrder == null){
                ztree.setChecked(false);
            }else{
                ztree.setChecked(true);
            }
            ztrees.add(ztree);
        });
        return ztrees;
    }

    /***
     * 勾选
     * @param omsDeptOrderStatus
     * @return
     */
    @PostMapping("add")
    @ResponseBody
    public AjaxResult addOrderStatus(@RequestBody OmsDeptOrderStatus omsDeptOrderStatus){
        try{
            orderStatusDeptService.insertStatus(omsDeptOrderStatus);
            return AjaxResult.success();
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /***
     * 取消勾选
     * @param omsDeptOrderStatus
     * @return
     */
    @PutMapping("/del")
    @ResponseBody
    public AjaxResult delOrderStatus(@RequestBody OmsDeptOrderStatus omsDeptOrderStatus){
        try{
            orderStatusDeptService.deleteStatus(omsDeptOrderStatus.getOrderStatus(), omsDeptOrderStatus.getDeptId());
            return AjaxResult.success();
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }
}
